﻿using System;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Collections;
using System.Data;
using System.Data.SqlClient;

using NickLee.Common;


namespace GPCSC.Web
{
    public partial class login : PageBase
    {
        public string enabled;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies["Cookie_Fortune"] != null)
                {
                    string values = HttpUtility.UrlDecode(Server.HtmlEncode(Request.Cookies["Cookie_Fortune"].Value));
                    username.Value = values;
                }
                if (ajaxlogin())
                {
                    this.Response.Redirect("mainFrom2.aspx", false);
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            #region 登陆Cookies设置

            if (Request.Cookies["Cookie_Fortune"] != null)
            {
                if (Request.Form["username"] != "")
                {
                    HttpCookie Cookies = new HttpCookie("Cookie_Fortune", HttpUtility.UrlEncode(username.Value));
                    Response.AppendCookie(Cookies);
                }
            }
            else
            {
                if (Request.Form["username"] != "")
                {
                    HttpCookie Cookies = new HttpCookie("Cookie_Fortune", HttpUtility.UrlEncode(username.Value));
                    Response.AppendCookie(Cookies);
                }
            }

            bool checkState = f_checkLogin(ref base.o_errMsg, pwd.Value.Trim(), username.Value.Trim());
            if (checkState)
            {
                if (enabled == "0")
                {
                    this.Response.Write("<script>top.location.href='index.aspx';</script>");
                }
                    else if(enabled=="1")
                {
                    this.Response.Redirect("SHFW/CLpy.aspx", false);
                    }

                else
                {
                    this.Response.Redirect("ErrPage.aspx", false);

                }
                
            }
            else
            {
                JSUtil.f_ajaxAlert(this, o_errMsg);
            }
            return;
            #endregion
        }
        public bool ajaxlogin()
        {
            if (Request.Cookies["Cookie_Fortune"] != null && Request.Cookies["Cookie_prass"] != null)
            {
                string username = HttpUtility.UrlDecode(Server.HtmlEncode(Request.Cookies["Cookie_Fortune"].Value));
                string parssword = HttpUtility.UrlDecode(Server.HtmlEncode(Request.Cookies["Cookie_prass"].Value));

                if (username != "" && parssword != "")
                {
                    bool checkState = f_checkLogin(ref base.o_errMsg, parssword.Trim(), username.Trim());
                    if (checkState)
                    {
                        return true;
                    }
                }
            }

            return false;
        }


        #region

        private bool f_checkLogin(ref string errMsg, string Pass, string User)
        {
            errMsg = User + ":";

            if (User == "")
            {
                errMsg = "请输入登录名!";
                return false;
            }

            if (Pass == "")
            {
                errMsg = "请输入密码!";
                return false;
            }
            Hashtable loginTable = new Hashtable();
            DataSet ldTable = new DataSet();
            SqlReadXml Org = new SqlReadXml();
            if (logins.Value == "1")
            {
                loginTable.Add("@USERNAME", User);
                loginTable.Add("@PWD", Pass);
                ldTable = Org.ReaderDataSet("sel_userlogin", loginTable);
            }
            else if (logins.Value == "2")
            {
                loginTable.Add("@Tel", User);
                loginTable.Add("@PWD", Pass);
                ldTable = Org.ReaderDataSet("sel_userloginTel", loginTable);
            }
                
            
            

            //存在一个用户多个部门的情况下
            if (ldTable.Tables[0].Rows.Count < 1)
            {
                errMsg = "登录名、登录密码不匹配,请重新输入!请注意登录名和密码的大小写!";
                return false;
            }
            else
            {

                #region 读取公共信息
                //读取用户信息
                GPCSC.Model.CL_USER_S user = new GPCSC.Model.CL_USER_S();
                ODRM.DataRowConvertObject(ldTable.Tables[0].Rows[0], user);
                enabled = ldTable.Tables[0].Rows[0]["ENABLED"].ToString();

                #region 新的用户相应信息存取


                ////完成最后的缓存哈希表填充
                Hashtable dTable = new Hashtable();
                loginTable.Clear();
                loginTable.Add("@USERID", user.CL_USER_SOID);

                DataTable userRoleTable = Org.ReaderDataSet("sel_userRole", loginTable).Tables[0];

                GPCSC.Model.CL_ROLE_S userRole = new GPCSC.Model.CL_ROLE_S();
                if (userRoleTable.Rows.Count > 0)
                {
                    ODRM.DataRowConvertObject(userRoleTable.Rows[0], userRole);
                }
                if (enabled == "0")
                {
                    #region 读取用户菜单信息、用户角色、用户岗位、用户部门
                    //读取用户菜单表中对应的菜单
                    DataTable userMenuTable = Org.ReaderDataSet("sel_usermenuByRoleID_all", loginTable).Tables[0];
                    GPCSC.Model.CL_Menu_S[] userMenu = new GPCSC.Model.CL_Menu_S[userMenuTable.Rows.Count];
                    if (userMenuTable.Rows.Count > 0)
                    {

                        for (int i = 0; i < userMenuTable.Rows.Count; i++)
                        {
                            userMenu[i] = new GPCSC.Model.CL_Menu_S();
                            ODRM.DataRowConvertObject(userMenuTable.Rows[i], userMenu[i]);
                        }
                    }


                    #endregion
                    dTable.Add("SYS_UserMenu", userMenu);
                
                }



                dTable.Add("SYS_USER", user);
                dTable.Add("SYS_userRole", userRole);
                if (o_baseTable.Count > 0)
                {
                    Session.Clear();
                    o_baseTable.Clear();
                }
                byte[] userinfo = CompressData.Compress(dTable);
                base.o_baseTable.Add(c_obj.s_userInfo, userinfo);
                Session[c_obj.s_baseTable] = base.o_baseTable;

                #endregion

                #endregion
                return true;


            }
        }

        #endregion

    }
}